package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-29
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        //获取文件输入流
        try {
            InputStream input = file.getInputStream();
            //调用方法读取数据
            EasyExcel.read(input, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //查询所有一级分类，parentId = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0"); //查询条件为parent_id为0

        //先查询出来
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //查询所有二级分类，parentId != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);



        //创建list集合，用于存储最终封装过的数据
        List<OneSubject> subjectList = new ArrayList<>();
        //封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值
        //封装到要求的list集合里面 List<OneSubject> SubjectList = new ArrayList<>();
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //得到oneSubjectList每个EduSubject对象中的值
            EduSubject eduSubject = oneSubjectList.get(i);
            //把eduSubject对象中的值取出来放到OneSubject对象里面
            //然后把OneSubject放到 subjectList 对象里面
            OneSubject oneSubject = new OneSubject();
            //oneSubject 对象取eduSubject 对象的值
                //第一种赋值方式
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
                //第二种赋值方式
                        BeanUtils.copyProperties(eduSubject,oneSubject);
            //再把oneSubject 对象赋值到 subjectList 集合当中即可式
            subjectList.add(oneSubject);


            //在一级分类循环遍历所有的二级分类
            //1.创建list集合封装每一个一级分类的二级分类
            ArrayList<TwoSubject> twoSubjects = new ArrayList<>();
            //使用for循环遍历二级分类
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每一个遍历出来的二级分类
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类的parent_id和一级分类的id是否一致
                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    //把tSubject对象的值赋值到TwoSubject里面，然后再添加到twoSubjects 集合中
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);

                    twoSubjects.add(twoSubject);
                }
            }

                //把一级分类下面的所有二级分类再放到一级分类里面
            oneSubject.setChildren(twoSubjects);
        }


        //返回一级分类的数据
        return subjectList;
    }
}
